package com.ztf.customerServiceThread;

import com.ztf.bean.DataTransfer;
import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.customer.Customer;
import com.ztf.gui.ChatRoom;
import com.ztf.gui.ImageListModel;
import com.ztf.gui.MsgListModel;

import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class CustomerAccept extends Thread{
    private ObjectInputStream ois;
    private boolean flag;

    public CustomerAccept(ObjectInputStream ois) {
        this.ois =ois;
        flag = true;
    }

    @Override
    public void run(){
        while (flag){
            try {
                DataTransfer o = (DataTransfer) ois.readObject();
                switch (o.getType()){
                    case "msg":
                        Message msg = (Message) o.getObject();

                        if (msg.getFrom().equals(ChatRoom.userNow)){
                            //如果是自己发的消息

                            ChatRoom.fr.get(msg.getTo()).add(msg);
                            MsgListModel mo1 = new MsgListModel();
                            for (Message message : ChatRoom.fr.get(msg.getTo())) {
                                mo1.addElement(message);
                            }
                            ChatRoom.chatRoom.list_1.setModel(mo1);
                            ChatRoom.chatRoom.repaint();
                        }else {
                            ChatRoom.fr.get(msg.getFrom()).add(msg);
                            if (ChatRoom.chatRoom.isSelected.equals(msg.getFrom())){
                                MsgListModel mo1 = new MsgListModel();
                                for (Message message : ChatRoom.fr.get(msg.getFrom())) {
                                    mo1.addElement(message);
                                }
                                ChatRoom.chatRoom.list_1.setModel(mo1);
                                ChatRoom.chatRoom.repaint();
                                ChatRoom.chatRoom.label7.setText("");
                            }
                        }
                        break;
                    case "client":
                        //客户连接
                        List<Object> user = (List<Object>) o.getObject();
                        List<Message> msgs = (List<Message>) user.get(0);
                        User to = (User) user.get(1);
                        to.setBeReceived(System.currentTimeMillis());
                        for (Message message : msgs) {
                            message.setTo(ChatRoom.userNow);
                        }
                        ChatRoom.fr.put(to,msgs);

                        loadList();
                        break;
                    case "offline":
                        //客户离线

                        //接待人数
                        ChatRoom.chatRoom.receivedPerson++;
                        ChatRoom.chatRoom.label4.setText(ChatRoom.chatRoom.receivedPerson+"");

                        User user2 = (User) o.getObject();
                        Set<User> users = ChatRoom.fr.keySet();
                        long time = 0;
                        for (User user1 : users) {
                            if (user1.equals(user2)){
                                time = user1.getBeReceived();
                                break;
                            }
                        }
                        List<Message> remove = ChatRoom.fr.remove(user2);
                        List<Object> list = new ArrayList<>();
                        list.add(user2.getName());
                        list.add(remove);
                        list.add(System.currentTimeMillis()-time);
                        System.out.println(remove.size());

                        Customer.send(new DataTransfer("saveMessage",list));

                        //清空当前面板聊天界面,如果退出的人是正在聊天的人
                        if (ChatRoom.chatRoom.isSelected.equals(user2)){
                            MsgListModel mo1 = new MsgListModel();
                            ChatRoom.chatRoom.list_1.setModel(mo1);
                            ChatRoom.chatRoom.isSelected=null;
                            ChatRoom.chatRoom.lblNewLabel.setText("");
                        }

                        loadList();
                        break;
                    case "waitUsers":
                        int wait = (int) o.getObject();
                        ChatRoom.chatRoom.label6.setText(wait+"");
                        break;
                    case "i_am_talking":
                        ChatRoom.chatRoom.label7.setText("对方正在输入");
                        break;
                }
            } catch (Exception e) {
                flag = false;
                System.out.println("接受错误");
                e.printStackTrace();
            }

        }
    }

    //加载好友列表
    public void loadList(){
        try {
            sleep(50);
            ImageListModel mo2 = new ImageListModel();
            Set<User> users = ChatRoom.fr.keySet();
            for (User user : users) {
                mo2.addElement(user);
            }
            ChatRoom.chatRoom.list.setModel(mo2);
            ChatRoom.chatRoom.repaint();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
